package com.orangelife.mobile.individual.activity;

import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.WebViewURL;
import com.thirdsdk.umeng.UMShare;
import com.thirdsdk.umeng.UmShareBiz;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.SelectPicPopupWindowView;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * 分享到社交平台
 *
 * @author cfr, yw
 */
public class ShareFriendActivity extends OrangeLifeBaseActivity {
    private WebView webView;
    private TextView tv_title, tv_title_right;
//    private String inviteCode;
    /** 弹出框 */
    SelectPicPopupWindowView menuWindow;
    /** -------------分享------------------------ */
    /** 首先在您的Activity中添加如下成员变量 */
    private final UMSocialService mController = UMServiceFactory
            .getUMSocialService("com.umeng.share");
    /** 分享url */
    private String targetUrl = Constant.DOWNLOAD_APK;/* 必须为http连接 */
    /** 分享title */
    private String uMtitle = "橙子生活";
    /** 分享content */
    private String uMcontent = "您的朋友邀请您一起体验橙子生活优质社区服务";
    private UMShare share;
    private SHARE_MEDIA platform;
    UmShareBiz shareBiz;



    /** ------------------------------------------ */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.js_transter_java2);
        initView();
    }

    private void initView() {
        initTitle();
        initWebView();
        initShare();
    }



    private void initTitle() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title_right = (TextView) findViewById(R.id.tv_title_right);
        tv_title_right.setVisibility(View.INVISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("我要分享");
    }

    private void initShare() {
        shareBiz = new UmShareBiz(this);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        webView = (WebView) findViewById(R.id.webview);
        webView.clearCache(true);
        webView.getSettings().setJavaScriptEnabled(true);
        String url =  WebViewURL.SHARE_FRIEND;
        webView.loadUrl(url);
        webView.setWebViewClient(new MyWebViewClient());
        webView.addJavascriptInterface(new ScriptInterface(this), "index");
    }


    class ScriptInterface {
        private Activity context;

        public ScriptInterface(Activity context) {
            this.context = context;
        }

        @android.webkit.JavascriptInterface
        public void share() {
            shareBiz.share(uMtitle, uMcontent, targetUrl, "");
        }
    }


    class MyWebViewClient extends WebViewClient {
        private String code;

        public MyWebViewClient() {
            showDialog("正在加载中...");
        }

        public MyWebViewClient(String code) {
            this.code = code;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        /**
         * 要等js全部加载完毕后，才能过调用js函数
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dismissDialog();
//            webView.loadUrl("javascript:setInviteCode(" + code + ")");
        }
    }

    /**
     * 处理返回的信息数据
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 500:
                    dismissDialog();
                    ToastHelper.getInstance()._toast(
                            msg.obj.toString());
                    break;
                case 1011:
                    isLogin(dialog);
                    finish();
                    break;
                default:
                    break;
            }
        }

    };

    /*##################dialog Start###################*/
    private Dialog dialog;

    private void showDialog(String msg) {
        if (msg == null) {
            msg = "正在请求数据，请稍后";
        }
        dialog = DialogHelper.getInstance().createLoadingDialog(this, msg);
        dialog.show();
    }

    private void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
     /*##################dialog end###################*/

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDialog();
    }
}
